/**
 * 定义校准设置模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.sw.calSetting.calSettingManager', ['water.common.dateUtil',
        'water.entity.bcMonitorParamDict',
        'water.entity.calSetting']);
		
	module.controller('calSettingManagerController',function($scope, $dateUtil,
                                                               $bcMonitorParamDictService,
                                                               $calSettingService){
		$scope.PARAM_CODE_BUS;

		$scope.modelList = [];

        /**
         * 小时选择器数据源
         */
        $scope.hours = [];

        $scope.getList = function(){
            $calSettingService.getList().then(function(data){
                $scope.modelList = data;
                for(var i=0;i<$scope.modelList.length;i++){
                    for(var j=0;j<$scope.paramData.length;j++){
                        if($scope.modelList[i].PARAM_CODE_BUS==$scope.paramData[j].k){
                            $scope.modelList[i].PARAM_CODE_BUS = $scope.paramData[j].v;
                        }
                    }
                }
            });
        };

        $scope.getParamData = function () {
            $bcMonitorParamDictService.getList().then(function (data) {
                var paramData = [];

                for (var i = 0; i < data.length; i++) {
                    paramData.push({
                        k: data[i].PARAM_CODE_BUS,
                        v: data[i].PARAM_NAME
                    });
                }
                $scope.paramData = paramData;
                $scope.paramDataModel.setData(paramData);
            });
        };

        $scope.onSetCalTimeClick = function () {
            var startTimeStr =$scope.startDate + " " +  $scope.startHour +  ":00:00";
            var endTimeStr = $scope.endDate + " " +  $scope.endHour +  ":59:59";

            var date = new Date();
            var start = new Date(Date.parse(startTimeStr));
            var end = new Date(Date.parse(endTimeStr));

            if(start>end){
                alert("开始时间不能大于结束时间");
                return;
            }

            if(start<date){
                alert("开始时间不能小于当前时间");
                return;
            }

            if($scope.PARAM_CODE_BUS==undefined){
                alert("监测项不能为空");
                return;
            }

            var model = {};
            model.START_TIME = startTimeStr;
            model.END_TIME = endTimeStr;
            model.PARAM_CODE_BUS = $scope.PARAM_CODE_BUS;
            $calSettingService.create(model).then(function(data){
                $scope.getList();
            });
        };


        /**
         * 初始化小时下拉选择数据
         */
        $scope.initHours = function(){
            for(var i=0;i<24;i++){
                $scope.hours.push({
                    k:i,
                    v:i+"时"
                });
            }
        }


        $scope._init = function(){

            var date = new Date();

            date = $dateUtil.addHour(date,1);

            $scope.startDate = $dateUtil.formatDate(date,'yyyy-MM-dd');
            $scope.startHour = date.getHours();

            $scope.endDate = $dateUtil.formatDate(date,'yyyy-MM-dd');
            $scope.endHour = date.getHours();

            $scope.initHours();
            $scope.getParamData();
            $scope.getList();
        }

        $scope._init();

		
	});
	
}(window, angular));





